package com.zthz.douxing.sendmessageservice.service

import com.zthz.douxing.PlanTripSaveType
import com.zthz.douxing.Plantrip
import com.zthz.douxing.SegmentStatus
import com.zthz.douxing.TaskInfo
import com.zthz.douxing.TripSegment
import com.zthz.douxing.event.UserWillDepartEvent
import com.zthz.douxing.msg.MessageType

class ChangeArriveStatusService extends AbstractHandlerService{


    @Override
    protected String hanldTask(TaskInfo taskInfo) throws Exception {

        String resultDesc = "更改状态成功"
        UserWillDepartEvent userWillDepartEvent = formateTask2UserWillDepartEvent(taskInfo)
        String statusMsgContent = null
        try {
            statusMsgContent = "更改行程的状态为在Arrived"
            TripSegment tripSegment = userWillDepartEvent.tripSegment
            tripSegment.segmentStatus = SegmentStatus.Arrived
            Plantrip plantrip = tripSegment.plantrip

            if(plantrip.endDate.getTime() == tripSegment.arriveTime.getTime())
                plantrip.planTripSaveType = PlanTripSaveType.DO_EXPIRED

            if(plantrip.save(flush: true)){
                if(!tripSegment.save(flush: true))
                    resultDesc = "更改状态失败"
            }else
                resultDesc = "更改状态失败"
        } catch (Exception e){
            resultDesc = "更改状态失败"
            throw e
        }finally{
            orderXingeMessageService.
                    saveMessageToUserLog(userWillDepartEvent,statusMsgContent,
                            resultDesc,MessageType.Arrieded)
        }
        return resultDesc
    }
}
